/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ugame.module.store;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import vn.ugame.model.bl.FindResult;
import vn.ugame.model.bl.ModuleManager;
import vn.ugame.model.bl.StoreService;
import vn.ugame.model.entity.Comment;
import vn.ugame.model.entity.Image;
import vn.ugame.model.entity.Item;
import vn.ugame.model.entity.Language;
import vn.ugame.module.*;

/**
 *
 * @author Tony
 */
public class StoreModule extends AbstractModule implements Module {

    public StoreModule() {
    }

    @ActionConfig(urlPattern = {"", "^(?<page>\\d+)/?$", "^(?<page>\\d+)/(?<keyword>[^/]+)/?$"})
    public void displayStore(@Param("keyword") String keyword, @Param("page") int page)
            throws ServletException, IOException {
        /**
         * Set default values for parameters
         */
        page = page <= 0 ? 1 : page;
        keyword = keyword != null && keyword.trim().equals("") ? null : keyword;

        /**
         * declare service
         */
        ModuleManager manager = new ModuleManager(getConfig().getLocale());
        StoreService service = new StoreService(getConfig().getLocale());

        /**
         * Prepare default model for store module
         */
        // Create default model
        List<vn.ugame.model.entity.Module> categories = manager.findModulesByType(ModuleType.STORE_SPECIFIC);
        List<vn.ugame.model.entity.Module> roots = manager.getRootModules();
        List<Language> languages = new ArrayList<Language>();
        List<vn.ugame.model.entity.Module> localModules = manager.findLocalModules(getConfig().getId());
        for (vn.ugame.model.entity.Module module : localModules) {
            vn.ugame.model.entity.Language language = new Language();
            if (module.getName().equals(getConfig().getName())) {
                language.setCurrent(true);
            } else {
                language.setCurrent(false);
            }
            language.setLocale(module.getLocale());
            language.setUrl("/" + module.getName() + "/" + getQueryString());
            languages.add(language);
        }
        //Save default model into request scope
        getRequest().setAttribute("config", getConfig());
        getRequest().setAttribute("roots", roots);
        getRequest().setAttribute("categories", categories);
        getRequest().setAttribute("languages", languages);

        /**
         * Prepare alternative model for display item action
         */
        // Create model
        FindResult<Item> model = service.findItems(getConfig().getId(), keyword, page);
        //Save model into request scope
        getRequest().setAttribute("model", model);

        /**
         * Forward model to view
         */
        RequestDispatcher dispatcher = getRequest().getRequestDispatcher("/store.jsp");
        dispatcher.forward(getRequest(), getResponse());
    }

    @ActionConfig(urlPattern = {"^(?<name>[a-zA-Z0-9_-]+)/(?<id>[a-zA-Z0-9_-]+)/?$"})
    public void displayItem(@Param("id") String id, @Param("name") String name)
            throws ServletException, IOException {

        /**
         * declare service
         */
        ModuleManager manager = new ModuleManager(getConfig().getLocale());
        StoreService service = new StoreService(getConfig().getLocale());

        /**
         * Prepare default model for store module
         */
        // Create default model
        List<vn.ugame.model.entity.Module> categories = manager.findModulesByType(ModuleType.STORE_SPECIFIC);
        List<vn.ugame.model.entity.Module> roots = manager.getRootModules();
        List<Language> languages = new ArrayList<Language>();
        List<Item> localItems = service.findLocalItems(id);
        for (Item localItem : localItems) {
            vn.ugame.model.entity.Language language = new Language();
            if (localItem.getName().equals(name)) {
                language.setCurrent(true);
            } else {
                language.setCurrent(false);
            }
            language.setLocale(localItem.getLocale());
            language.setUrl("/" + localItem.getModule().getName()
                    + "/" + localItem.getName() + "/" + localItem.getId());
            languages.add(language);
        }
        //Save default model into request scope
        getRequest().setAttribute("config", getConfig());
        getRequest().setAttribute("roots", roots);
        getRequest().setAttribute("categories", categories);
        getRequest().setAttribute("languages", languages);

        /**
         * Prepare alternative model for display item action
         */
        // Create model
        Item item = service.findItem(id, name);
        //Process item not found exception
        if(item == null){
            getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        List<Image> screenShots = service.getScreenShots(id);
        List<Comment> comments = service.findComments(id);
        //Save model into request scope
        getRequest().setAttribute("item", item);
        getRequest().setAttribute("screenShots", screenShots);
        getRequest().setAttribute("comments", comments);

        /**
         * Forward model to view
         */
        RequestDispatcher dispatcher = getRequest().getRequestDispatcher("/item.jsp");
        dispatcher.forward(getRequest(), getResponse());
    }
}
